package tags;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.TagSupport;


public class WeekdayTag extends TagSupport {
	private static final long serialVersionUID = 6328822983290086431L;

	static final String[] WD = { "", "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat" };
	private String date;

	public void setDate(String date) {
		this.date = date;
	}
	
	@Override
	public int doEndTag() throws JspException {
		GregorianCalendar cal = new GregorianCalendar();
		SimpleDateFormat fmt = new SimpleDateFormat("yyyy-MM-dd");
		fmt.setLenient(true);
		if (date != null && date.length() > 0) {
			Date d = new Date();
			try {
				d = fmt.parse(date);
			} catch (Exception e) {
				throw new JspException("Date parsing failed: " + e.getMessage());
			}
			cal.setTime(d);
		}
		try {
			pageContext.getOut().print(WD[cal.get(Calendar.DAY_OF_WEEK)]);
		} catch (Exception e) {
			throw new JspException("Weekday writing failed: " + e.getMessage());
		}
		return EVAL_PAGE;
	}
}







